package com.midea.ai.air.view;

import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.midea.ai.air.R;
import com.midea.ai.util.share.ShareUtils;

/**
 * Created by hww on 2016/4/19.
 * 实现分享的View
 */
public class ShareView implements View.OnClickListener{
    private Context mContext;
    private Dialog mDialog;
    private View mShareView; //要分享的View

    public ShareView(Context context) {
        mContext = context;
        init();
    }

    public void init() {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.dialog_share, null);
        mDialog = new Dialog(mContext, R.style.my_activate_dialog);
        mDialog.setCancelable(true);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setContentView(view);
        LinearLayout share_qq = (LinearLayout) view.findViewById(R.id.dialog_share_qq);
        LinearLayout share_wx = (LinearLayout) view.findViewById(R.id.dialog_share_wx);
        LinearLayout share_pyq = (LinearLayout) view.findViewById(R.id.dialog_share_pyq);
        LinearLayout share_wb = (LinearLayout) view.findViewById(R.id.dialog_share_wb);

        mDialog.show();
        share_qq.setOnClickListener(this);
        share_wx.setOnClickListener(this);
        share_pyq.setOnClickListener(this);
        share_wb.setOnClickListener(this);
    }

    /**
     * 对需要的view截图为bitmap
     */
    private static Bitmap loadBitmapFromView(View view) {
        if (view == null) {
            return null;
        }
        Bitmap screenshot;
        screenshot = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(screenshot);
        c.drawColor(0xFF000000);
        view.draw(c);
        return screenshot;
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        try {
            Bitmap b = loadBitmapFromView(getShareView());
            switch (viewId) {
                case R.id.dialog_share_qq:
                    ShareUtils.shareToQQFriend(mContext, null, b, "来自美的空气的分享");
                    break;

                case R.id.dialog_share_wx:
                    ShareUtils.shareToWeixinFriend(mContext, null, b, "来自美的空气的分享");
                    break;

                case R.id.dialog_share_pyq:
                    ShareUtils.shareToWeixinTimeLine(mContext, null, b, "来自美的空气的分享");
                    break;

                case R.id.dialog_share_wb:
                    ShareUtils.shareToWeibo(mContext, null, b, "来自美的空气的分享");
                    break;
            }
        } catch (ActivityNotFoundException e) {
            String str = "您没有安装微信";
            if (viewId == R.id.dialog_share_qq) {
                str = "您没有安装QQ";
            } else if (viewId == R.id.dialog_share_wb) {
                str = "您没有安装微博";
            }
            Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(mContext, "分享失败", Toast.LENGTH_SHORT).show();
        }
        mDialog.dismiss();
    }

    public View getShareView() {
        return mShareView;
    }

    public void setShareView(View shareView) {
        mShareView = shareView;
    }
}
